#!/usr/bin/python
# -*- coding: UTF-8 -*-
import requests
import json
import notify2
import time
from datetime import datetime
def get_product_label_id():
#cron:0 0-23/6 * * *
 """
 从给定的商城相关数据字典中提取productLabelName为"积分礼品"的productLabelId
 :param data: 包含商城信息的字典数据
 :return: 对应的productLabelId或者None（如果没找到）
 """
 headers = {
  'sec-ch-ua-platform': '"Android"',
  'x-csrf-token': 'Hzast_rqxaRiQPJnraSjs9XA',
  'x-kish-token-key': 'token',
  'sec-ch-ua': '"Microsoft Edge";v="131", "Chromium";v="131", "Not_A Brand";v="24"',
  'sec-ch-ua-mobile': '?1',
  'baggage': 'sentry-environment=PRO,sentry-release=seewoedu-college-v3-mobile-997e0de2,sentry-public_key=a47500a7302c47b9bda4fe40bd6451cb,sentry-trace_id=bd7bc2ce295449ea84ef852b5ded6de7,sentry-sample_rate=0.4',
  'sentry-trace': 'bd7bc2ce295449ea84ef852b5ded6de7-b7a7fa1e97b7aa8b-0',
  'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Mobile Safari/537.36 Edg/131.0.0.0',
  'accept': 'application/json, text/plain, */*',
  'sec-fetch-site': 'same-origin',
  'sec-fetch-mode': 'cors',
  'sec-fetch-dest': 'empty',
  'referer': 'https://m.seewoedu.cn/college/exchange/home',
  'accept-encoding': 'gzip, deflate, br, zstd',
  'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
  'priority': 'u=1, i',
  'cookie': 'acw_tc=1a0c5dd717318925528291633e00428eb0641aa9080d1f97e12b913bb459a5; csrfToken=Hzast_rqxaRiQPJnraSjs9XA; Hm_lvt_28e60218260284b8df3efca2fc9f65ad=1731892535; HMACCOUNT=5FC105B8708438D9; isLogin=false; ab_uid=07aa1aaba264bfce1e97bbb6d0b16784; prfb=97jjZwValvsycULuD2nMIiqzaYI1CeQaT6u1s7bx8zEI1JG6k4jTx8jBhv2B/4m5; prfb.sig=4MIQNpu3NLJz9EMQ-6QLNbpv3pXc7THN1HJCTghRIEA; prfbe=1; prfbe.sig=YigtUW3dSaV9Z4-j9W4uoyH2ZwLPlLNJzZCP_Mnfdt4; Hm_lpvt_28e60218260284b8df3efca2fc9f65ad=1731892561'}
 payload = None
 data = requests.request("GET","https://m.seewoedu.cn/apiv3/v1/cms/channels/tradeChannel/modules?time=1731892560544-rbcly9iu78j-rTTcadmgj",headers=headers, data=payload)
 #print(response0.text)
 modules = data.json()["data"]["modules"]
 for module in modules:
  if module["moduleName"] == "栏目分类标签":
   module_datas = module["moduleDatas"]
   for item in module_datas:
    if item["productLabelName"] == "积分礼品":
     return item["productLabelId"]
 return None

#print(get_product_label_id())
def huoqushangping():
 headers = {
  'sec-ch-ua-platform': '"Android"',
  'x-csrf-token': 'Hzast_rqxaRiQPJnraSjs9XA',
  'x-kish-token-key': 'token',
  'sec-ch-ua': '"Microsoft Edge";v="131", "Chromium";v="131", "Not_A Brand";v="24"',
  'sec-ch-ua-mobile': '?1',
  'baggage': 'sentry-environment=PRO,sentry-release=seewoedu-college-v3-mobile-997e0de2,sentry-public_key=a47500a7302c47b9bda4fe40bd6451cb,sentry-trace_id=bd7bc2ce295449ea84ef852b5ded6de7,sentry-sample_rate=0.4',
  'sentry-trace': 'bd7bc2ce295449ea84ef852b5ded6de7-8731cede0abf3b4e-0',
  'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Mobile Safari/537.36 Edg/131.0.0.0',
  'accept': 'application/json, text/plain, */*',
  'sec-fetch-site': 'same-origin',
  'sec-fetch-mode': 'cors',
  'sec-fetch-dest': 'empty',
  'referer': 'https://m.seewoedu.cn/college/exchange/home',
  'accept-encoding': 'gzip, deflate, br, zstd',
  'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
  'priority': 'u=1, i',
  'cookie': 'acw_tc=1a0c5dd717318925528291633e00428eb0641aa9080d1f97e12b913bb459a5; csrfToken=Hzast_rqxaRiQPJnraSjs9XA; Hm_lvt_28e60218260284b8df3efca2fc9f65ad=1731892535; HMACCOUNT=5FC105B8708438D9; isLogin=false; ab_uid=07aa1aaba264bfce1e97bbb6d0b16784; prfb=97jjZwValvsycULuD2nMIiqzaYI1CeQaT6u1s7bx8zEI1JG6k4jTx8jBhv2B/4m5; prfb.sig=4MIQNpu3NLJz9EMQ-6QLNbpv3pXc7THN1HJCTghRIEA; prfbe=1; prfbe.sig=YigtUW3dSaV9Z4-j9W4uoyH2ZwLPlLNJzZCP_Mnfdt4; Hm_lpvt_28e60218260284b8df3efca2fc9f65ad=1731892561'}
 payload = None
 response = requests.request("GET",f"https://m.seewoedu.cn/apiv3/v1/trade/products?productLabelId={get_product_label_id()}&page=0&size=10&time=1731892560711-ZRuyGt_KLwjP_WNCQVu0i",headers=headers, data=payload)
 #print(response.text)
 contents = ""
 # 发送网络请求获取JSON数据（这里假设接口地址为https://example.com/api/data，实际中要替换为真实地址）
 #response = requests.get('https://example.com/api/data')
 if response.status_code == 200:
  data = json.loads(response.text)
  products = data["data"]["content"]
  for product in products:
   title = product["title"]
   integration_price = product["integrationPrice"]
   contents += f"{title}: {integration_price}\n\n"
 else:
  contents = f"请求失败，状态码: {response.status_code}"
 return contents
 #print(contents)

a = huoqushangping()
b = 1
while b<24:
    result = huoqushangping()
    current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    if result!= a:
        title = "希沃学院商品通知"
        #contents = huoqushangping
        #print(contents)
        notify2.send(title,result)
        a = result
        #has_printed = False  # 当结果不同，更新a的值，并重置has_printed为False
    else:
        print(f"本次结果与上次相同，这是第{b}次运行，时间为：{current_time}")
        b=b+1
        print(f'本次结果是：\n{result}')
        #has_printed = True  # 输出信息后，将has_printed置为True，表示已输出过该信息
    print('30分钟后继续对比！')
    time.sleep(30 * 60)
    